package com.design.proxy.dynamicproxy.advanced1.mq2;

import java.lang.reflect.Proxy;

public class BaseConsumerProxy {

    private Object target;

    private MsgLogServiceImpl msgLogService;

    public BaseConsumerProxy(Object target, MsgLogServiceImpl msgLogService) {
        this.target = target;
        this.msgLogService = msgLogService;
    }

    public Object getProxy() {
        ClassLoader classLoader = target.getClass().getClassLoader();
        Class[] interfaces = target.getClass().getInterfaces();

        Object proxy = Proxy.newProxyInstance(classLoader, interfaces, (proxy1, method, args) -> {
            String s1 = (String) args[0];
            String s2 = (String) args[1];
            try {
                System.out.println("start...");
                Object result = method.invoke(target, args);// 真正消费的业务逻辑
//                msgLogService.updateStatus(correlationId, Constant.MsgLogStatus.CONSUMED_SUCCESS);
                System.out.println("stop...");
                return result;
            } catch (Exception e) {
                return null;
            }
        });
        return proxy;
    }
}
